package bamboo.drawingkid;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import bamboo.drawingkid.controls.DrawingViewController;
import bamboo.drawingkid.controls.MmsEmailViewController;
import bamboo.drawingkid.views.DrawingView;
import bamboo.drawingkid.views.MmsEmailView;
import bamboo.drawingkid.views.components.FormTemplate;

/**
 * @author Cong Ho
 *
 */
public class Main extends MIDlet implements CommandListener {

	private static Main self;
    private Display display;
    private DrawingView vwDrawing;
    private MmsEmailView vwMmsEmail;
    private Alert error;
   // private FormTemplate formTemplate;

    public static Main getInstance() {
        return self;
    }

    public Display getDisplay()
    {
    	return display;
    }
    
    public void startApp() {
    	//formTemplate = new FormTemplate("");
    	if (display == null) {
            try {
            	self = this;
	            display = Display.getDisplay(this);
	            vwDrawing = DrawingViewController.getInstance();
	            vwMmsEmail = MmsEmailViewController.getInstance();
	            display.setCurrent(vwDrawing);
	            error = new Alert("Error!", "Error happened.", null, AlertType.ERROR);
	            error.setTimeout(Alert.FOREVER);
	            error.setCommandListener(this);
	            System.out.println(getJavaVersion());
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showDrawing() {
        display.setCurrent(vwDrawing);
    }

    public void showMmsEmailView() {
		display.setCurrent(vwMmsEmail);
	}
   /* public void showTemplateView(){
    	display.setCurrent(formTemplate);
    }*/

    public DrawingView getDrawArea() {
        return vwDrawing;
    }

    public void showError(String errorMessage) {
        error.setString(errorMessage);
        display.setCurrent(error, vwDrawing);
    }

    public void commandAction(Command cmnd, Displayable dsplbl) {
    	if( cmnd == Alert.DISMISS_COMMAND) {
            display.setCurrent(vwDrawing);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public static String getJavaVersion() {
        String platformString = System.getProperty("microedition.platform");
        String version = "";
        int index = 0;
        index = platformString.toLowerCase().indexOf("java_build_version");
        if (index != 0) {
            version = platformString.substring(index + 19);
        } else {
            version = "N/A";
        }
        return version;
    }
   
}
